package com.example.map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;


public class MapActivity extends Activity {

        GestureDetector         gestureDetector = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        List<Planet> planets = new ArrayList<Planet>();
        planets.add(new Planet(41.90, 46.97));
        planets.add(new Planet(38.85, 25.37));
        planets.add(new Planet(21.24, 40.80));
        planets.add(new Planet(30.76, 35.69));
        planets.add(new Planet(13.77, 60.13));
        planets.add(new Planet(14.13, 45.88));
        planets.add(new Planet(48.67, 50.15));
        planets.add(new Planet(50.89, 38.35));
        planets.add(new Planet(49.13, 4.64));
        planets.add(new Planet(49.13, 4.64));
        planets.add(new Planet(53.28, 25.89));
        planets.add(new Planet(21.65, 17.70));
        planets.add(new Planet(44.46, 54.23));
        planets.add(new Planet(6.43, 60.43));
        planets.add(new Planet(63.21, 9.66));


        final CustomDrawableView customDrawableView = new CustomDrawableView(this.getApplicationContext(), planets);

        setContentView(customDrawableView);

        gestureDetector = new GestureDetector(new MyGestureDetector());


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

}

class CustomDrawableView extends View {


    private ShapeDrawable mDrawable;

    public int x = 10;
    public int y = 10;
    public int width = 50;
    public int height = 50;

    private Canvas canvas;


    public CustomDrawableView(Context context, List<Planet> planets) {
        super(context);

        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(x, y, x + width, y + height);

    }

/*
    public CustomDrawableView(Context context) {
        super(context);

        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(x, y, x + width, y + height);

*/
/*

        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("CustomDrawableView", "onTouch");
                if (gestureDetector.onTouchEvent(event)) {

                    return true;
                }
                return false;
            }
        };
*//*



    }


*/

    protected void onDraw(Canvas canvas) {
        this.canvas = canvas;
        mDrawable.draw(canvas);
    }

    public ShapeDrawable getmDrawable() {
        return mDrawable;
    }


}

  class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {

      private static final int SWIPE_MIN_DISTANCE = 5;
      private static final int SWIPE_MAX_OFF_PATH = 250;
      private static final int SWIPE_THRESHOLD_VELOCITY = 5;
      private GestureDetector gestureDetector;
      View.OnTouchListener gestureListener;


        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.i("MyGestureDetector", "onFling");

            try {

                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
                    return false;
                }

                // right / left
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    // left
                     Log.i("MyGestureDetector","left");

               //     mDrawable.setBounds(x - 1, y, x + width, y + height);

                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    // right
                    Log.i("MyGestureDetector","right");
                 //   mDrawable.setBounds(x + 1, y, x + width, y + height);
                }


               // invalidate();
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }


/*


 #      X      Y
 2  41.90  46.97
 3  38.85  25.37
 4  21.24  40.80
 5  30.76  35.69
 6  13.77  60.13
 7  14.13  45.88
 8  48.67  50.15
10  50.89  38.35
11  49.13   4.64
12  52.28  25.89
14  21.65  17.70
15  44.46  54.23
16   6.43  60.43
17  63.21   9.66


 */

class Planet {
    public double x;
    public double y;

    public Planet(double x, double y) {
        this.x = x;
        this.y = y;
    }
}